<?php

/**
 * @content: 接收上传过来的护理统计数据
 * @Author:  Groube
 * @Date:    2017-03-09 18:00:00
 * @version  $Id$
 */
class HulitongjiDataUploadController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }


    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/09 17:40
     */
    function indexAction(){
        $request = $this->getRequest();
        $data = $request->getPost('data','');
        $user_department_id = $request->getPost('user_department_id','');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true'){
            //分表机制开启
            $zhuyuan_hulitongji_jilu = "zhuyuan_hulitongji_jilu_".$user_department_id;
        }else{
            $zhuyuan_hulitongji_jilu = "zhuyuan_hulitongji_jilu";
        }

        /*是否开启压缩 默认开启*/
        $compression_http_data = $request->getPost('compression_http_data','off');

        //解压缩数据
        $upload_data = json_decode(Utility::uncompress($data,$compression_http_data),true);

        if(empty($upload_data)){
            Utility::apiJsonError(40018,'(Method:接收上传过来的护理统计数据)数据不能为空!');
        }else{
            if(!is_array($upload_data)) {
                Utility::apiJsonError(40019,'(Method:接收上传过来的护理统计数据)传入的json格式的字符串有误!');
            }
        }

        foreach($upload_data as $one){
            if(Yaconf::get("config$yiyuan_id.hospital_name") == "富源县中医医院"){
                if($one['yizhu_zuhao']){
                    $zhuyuan_yizhu_changqi_result = DB::select('his_zhenliao_bianma')->from('zhuyuan_yizhu_changqi')
                        ->where('yiyuan_id','=',$yiyuan_id)
                        ->where('zuhao','=',$one['yizhu_zuhao'])
                        ->execute()
                        ->as_array();

                    $his_zhenliao_bianma = $zhuyuan_yizhu_changqi_result[0]['his_zhenliao_bianma'];
                    if(empty($his_zhenliao_bianma)){
                        $zhuyuan_yizhu_linshi_result = DB::select('his_zhenliao_bianma')->from('zhuyuan_yizhu_linshi')
                            ->where('yiyuan_id','=',$yiyuan_id)
                            ->where('zuhao','=',$one['yizhu_zuhao'])
                            ->execute()
                            ->as_array();

                        $his_zhenliao_bianma = $zhuyuan_yizhu_linshi_result[0]['his_zhenliao_bianma'];
                    }
                    $data_list = array(''=>'',
                        '0001526'=>'静脉输液(第一组)',
                        '脾胃病科0000000108'=>'静脉输液(第一组)',
                        '0001527'=>'静脉输液(第一组)',
                        '0001532'=>'静脉输液(第二组起)',
                        '0001528'=>'静脉输液(第二组起)',
                        '0001531'=>'小儿头皮静脉输液(第一组)',
                        '0001516'=>'皮试',
                        '000100'=>'皮试',
                        '脾胃病科0000000097'=>'皮试',
                        '0001554'=>'雾化吸入',
                        '8880018'=>'雾化吸入',
                        '0001863'=>'穴位注射',
                        '0001908'=>'穴位注射',
                        '0001537'=>'静脉穿刺置管术',
                        '脾胃病科0000000119'=>'静脉穿刺置管术',
                        '0001517'=>'静脉注射',
                        '脾胃病科0000000099'=>'静脉注射',
                        '0001515'=>'肌肉注射',
                        '0101002'=>'皮下注射',
                        'GYTJ0001'=>'皮下注射',
                        'Z100'=>'指脉氧测定',
                        '0001905'=>'中药口服',
                        '0001847'=>'中药热奄包治疗',
                        '0001852'=>'中药熏洗',
                        '880023'=>'中药熏洗',
                        '0001874'=>'拔罐疗法',
                        '0001570'=>'导尿',
                        '0001769'=>'血氧饱和度监测',
                        'Z13'=>'低频脉冲电治疗',
                        '0001841'=>'电针',
                        '0001524'=>'动脉采血器采血',
                        'JCL0000001568'=>'动态血压监测',
                        '0001876'=>'督灸',
                        '0000001581'=>'耳针',
                        '0001860'=>'耳针',
                        '0001574'=>'肛管排气',
                        '0001906'=>'肛门塞药',
                        '0001873'=>'隔物灸法',
                        '000'=>'刮痧呼吸通气',
                        'Z03'=>'关节腔灌注治疗',
                        '0001830'=>'红外线治疗',
                        'YW5'=>'红外线治疗',
                        '0000001579'=>'火针',
                        '0001553'=>'机械辅助排痰',
                        '0001883'=>'急性腰扭伤推拿治疗',
                        '0001881'=>'肩周炎推拿治疗',
                        '0001880'=>'颈椎病推拿治疗',
                        '0001519'=>'静脉采血器采血',
                        '脾胃病科0000000101'=>'静脉采血器采血',
                        '8800012'=>'静脉配液',
                        '0001529'=>'静脉输血',
                        '0001896'=>'静脉输血',
                        '0001518'=>'静脉注射器采血',
                        '脾胃病科0000000100'=>'静脉注射器采血',
                        '0001872'=>'灸法',
                        '00000019999'=>'口服',
                        '0001838'=>'蜡疗',
                        '0001877'=>'雷火灸',
                        '0001564'=>'冷热湿敷',
                        '0001571'=>'留置导尿',
                        '0001879'=>'落枕推拿治疗',
                        '0001886'=>'内科疾病推拿治疗',
                        '0001572'=>'膀胱冲洗',
                        '0001913'=>'膀胱冲洗',
                        '0001843'=>'偏瘫肢体综合训练',
                        '0001858'=>'普通针刺',
                        '0001887'=>'其他推拿治疗',
                        '0001840'=>'气压治疗',
                        '0001839'=>'牵引',
                        '88000011'=>'浅静脉置管护理',
                        '0001568'=>'灌肠',
                        '0101001'=>'灌肠',
                        '0001567'=>'灌肠',
                        '0101003'=>'灌肠',
                        '0001562'=>'特殊物理降温',
                        '0001844'=>'贴敷疗法',
                        '0001882'=>'网球肘推拿治疗',
                        '0001559'=>'胃肠减压',
                        '0001530'=>'微量泵、输液泵使用费',
                        '脾胃病科0000000112'=>'微量泵、输液泵使用费',
                        '120100011'=>'吸痰护理',
                        '0001885'=>'膝关节骨性关节炎推拿治疗',
                        '0001560'=>'洗胃',
                        '0001912'=>'小针刀治疗',
                        '0001866'=>'心电监测',
                        '0001770'=>'心脏电除颤术',
                        '0001867'=>'穴位贴敷治疗',
                        '0001865'=>'穴位自血疗法',
                        '0001884'=>'腰椎间盘突出推拿治疗',
                        'ZLF001'=>'遥测心电监护',
                        '0001561'=>'一般物理降温',
                        'ZLL001'=>'胰岛素泵持续皮下注射胰岛素',
                        '0001555'=>'鼻饲管置管',
                        '0001556'=>'鼻饲管注食',
                        '0001557'=>'鼻饲管注药',
                        '0001875'=>'游走罐',
                        '0000673'=>'大抢救',
                        '0000674'=>'中抢救',
                        '0000675'=>'小抢救',
                        '0000000083'=>'病危',
                        '0000000071'=>'病重',
                        'ZSX0005'=>'Ⅰ级护理(优质护理)',
                        '脾胃病科0000000067'=>'Ⅰ级护理(优质护理)',
                        'ZSX0004'=>'Ⅱ级护理(优质护理)',
                        '脾胃病科0000000069'=>'Ⅱ级护理(优质护理)',
                        'HU0003'=>'Ⅰ级护理',
                        'HU0004'=>'Ⅱ级护理',
                        'HU0005'=>'Ⅲ级护理',
                        '880001'=>'一级护理',
                        '8800002'=>'二级护理',
                        '8800003'=>'三级护理',
                        '0001833'=>'中频脉冲电治疗'
                    );
                    if(isset($data_list[$his_zhenliao_bianma])){
                        $one['tongji_item_name'] = $data_list[$his_zhenliao_bianma];
                    }else{
                        $one['tongji_item_name'] = "其他";
                    }

                }
                $insert_data[] = array(
                    'tongji_item_id' => trim($one['tongji_item_id']),
                    'tongji_item_name' => trim($one['tongji_item_name']),
                    'record_time' => trim($one['record_time']),
                    'hushi_id' => trim($one['hushi_id']),
                    'hushi_name' => trim($one['hushi_name']),
                    'user_department' => trim($one['user_department']),
                    'zhuyuan_id' => trim($one['zhuyuan_id']),
                    'tongji_item_type' => trim($one['tongji_item_type']),
                    'real_time' => trim($one['real_time']),
                    'yizhu_zuhao' =>  trim($one['yizhu_zuhao']),
                    'yiyuan_id' => $yiyuan_id
                );
            }else{
                $insert_data[] = array(
                    'tongji_item_id' => trim($one['tongji_item_id']),
                    'tongji_item_name' => trim($one['tongji_item_name']),
                    'record_time' => trim($one['record_time']),
                    'hushi_id' => trim($one['hushi_id']),
                    'hushi_name' => trim($one['hushi_name']),
                    'user_department' => trim($one['user_department']),
                    'zhuyuan_id' => trim($one['zhuyuan_id']),
                    'tongji_item_type' => trim($one['tongji_item_type']),
                    'real_time' => trim($one['real_time']),
                    'yiyuan_id' => $yiyuan_id
                );
            }
        }

        $db = Kohana_Database::instance();
        $db->begin();
        $status = 'ok';
        try{
            Utility::multiInsert($zhuyuan_hulitongji_jilu,$insert_data);
            $db->commit();
        }catch (Kohana_Exception $e){
            $db->rollback();
            $status = 'error';
        }

        $statr = array('state' => $status);
        $statr['error']  = "200";
        $statr['msg']  = "";
        $async_result_json = json_encode($statr);

        // 压缩数据
        $async_result_json = Utility::compress($async_result_json,$compression_http_data);
        echo $async_result_json;
    }
}